package com.keyue.controller;

import com.keyue.common.annotation.AnonymousAccess;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.vo.admin.*;
import com.keyue.log.Log;
import com.keyue.security.TokenProvider;
import com.keyue.security.service.OnlineUserService;
import com.keyue.service.admin.AdminUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/keyue/admin/auth")
public class LoginController {

    @Resource
    private AdminUserService userService;

    @Resource
    private OnlineUserService onlineUserService;

    @Resource
    private TokenProvider tokenProvider;

    /**
     * 手机注册
     *
     * @param registerVO 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @ApiOperation("手机注册")
    @PostMapping("/phone_register")
    public ServerResult phoneRegister(@RequestBody @Validated AdminUserRegisterVO registerVO) {
        return userService.phoneRegister(registerVO);
    }

    /**
     * 手机登录
     *
     * @param loginByPhoneVO 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @ApiOperation("手机登录")
    @PostMapping("/login_by_phone")
    public ServerResult loginByPhone(@RequestBody @Validated AdminUserLoginByPhoneVO loginByPhoneVO) {
        return userService.loginByPhone(loginByPhoneVO);
    }

    /**
     * 密码登录
     *
     * @param loginByPwdVO 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @Log("用户登录")
    @ApiOperation("用户登录")
    @PostMapping("/login_by_pwd")
    public ServerResult loginByPwd(@RequestBody @Validated AdminUserLoginByPwdVO loginByPwdVO) {
        return userService.loginByPwd(loginByPwdVO);
    }

    /**
     * 发送密码找回邮件
     *
     * @param recoverEmailVO 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @PostMapping("/send_password_recover_mail")
    public ServerResult sendPasswordRecoverMail(@RequestBody @Validated AdminUserPasswordRecoverEmailVO recoverEmailVO) {
        return userService.sendPasswordRecoverMail(recoverEmailVO);
    }

    /**
     * 密码修改
     *
     * @param modifyPwdVO 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @ApiOperation("密码修改")
    @PostMapping("/password_modify")
    public ServerResult passwordModify(@RequestBody @Validated AdminUserModifyPwdVO modifyPwdVO) {
        return userService.passwordModify(modifyPwdVO);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return 查询结果
     */
    @AnonymousAccess
    @ApiOperation("退出登录")
    @PostMapping("/login_out")
    public ServerResult loginOut(HttpServletRequest request) {
        onlineUserService.logout(tokenProvider.getToken(request));
        return ServerResult.success();
    }
}
